<?php

/**
 * ImageDAO
 *
 */
class ImageDAO
{
  /**
   * Return all category name
   *
   */
  public function findAllByCategory($cateId)
  {
    $db = Yii::app()->db;
    $deleteFlag = Constants::DELETE_FLAG_FALSE;

    $sql = 'SELECT image_id,thumb,create_date '
         . 'FROM image '
         . 'WHERE delete_flag = :delete_flag '
         . 'AND category_id = :category_id '
         . 'ORDER BY image_id ASC';

    $command = $db->createCommand($sql);
    $command->bindParam(':delete_flag', $deleteFlag);
    $command->bindParam(':category_id', $cateId);

    $result = $command->queryAll();

    return $result;
  }

  public function findById($imageId)
  {
    $db = Yii::app()->db;
    $deleteFlag = Constants::DELETE_FLAG_FALSE;

    $sql = 'SELECT image_id,category_id,path,thumb,create_date,update_date,delete_flag '
    . 'FROM image '
    . 'WHERE delete_flag = :delete_flag '
    . 'AND image_id = :image_id '
    . 'ORDER BY image_id ASC';

    $command = $db->createCommand($sql);
    $command->bindParam(':delete_flag', $deleteFlag);
    $command->bindParam(':image_id', $imageId);
    $result = $command->queryAll();

    if ($result) {
      return $result[0];
    }

    return NULL;
  }

  public function insert($cateId, $path, $thumb) {
      $db = Yii::app()->db;
      $deleteFlag = Constants::DELETE_FLAG_TRUE;

      $sql = 'INSERT INTO image(category_id,path,thumb,create_date,update_date) VALUES '
            .'(:cateId,:path,:thumb,NOW(),NOW())';

      $command = $db->createCommand($sql);
      $command->bindParam(':cateId', $cateId);
      $command->bindParam(':path', $path);
      $command->bindParam(':thumb', $thumb);
      $command->execute();

      return TRUE;
  }
}